<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('operation_logs', function (Blueprint $table) {
            // 操作类型：create, update, delete, export, import, view等
            $table->string('action', 50)->nullable()->after('method')->comment('操作类型');
            // 资源类型：users, roles, menus等
            $table->string('resource', 50)->nullable()->after('action')->comment('资源类型');
            // 资源ID
            $table->unsignedBigInteger('resource_id')->nullable()->after('resource')->comment('资源ID');
            // 操作描述
            $table->string('description')->nullable()->after('resource_id')->comment('操作描述');
            // 修改前的数据（JSON）
            $table->json('before_data')->nullable()->after('description')->comment('修改前数据');
            // 修改后的数据（JSON）
            $table->json('after_data')->nullable()->after('before_data')->comment('修改后数据');
            // 变更字段（JSON，记录哪些字段被修改了）
            $table->json('changes')->nullable()->after('after_data')->comment('变更字段');
            
            // 添加索引
            $table->index('action');
            $table->index('resource');
            $table->index(['resource', 'resource_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('operation_logs', function (Blueprint $table) {
            $table->dropIndex(['resource', 'resource_id']);
            $table->dropIndex(['resource']);
            $table->dropIndex(['action']);
            
            $table->dropColumn([
                'action',
                'resource',
                'resource_id',
                'description',
                'before_data',
                'after_data',
                'changes',
            ]);
        });
    }
};
